import { createStore } from "vuex";

const store= createStore({
  state: {
    user: null,
    token: sessionStorage.getItem('token') ? sessionStorage.getItem('token'):'',
    tableData: [],
    username: localStorage.getItem('username') ? localStorage.getItem('username'):'',
    passwordMD5: localStorage.getItem('passwordMD5') ? localStorage.getItem('passwordMD5'):'d41d8cd98f00b204e9800998ecf8427e',
    chooseSavePassword: JSON.parse(localStorage.getItem('chooseSavePassword')) ? JSON.parse(localStorage.getItem('chooseSavePassword')): false,
  },
  mutations: {
    setUser(state, user){
      state.user = user;
    },
    setToken(state, token){
      state.token = token;
      sessionStorage.setItem('token', token)
    },
    set_tableData(state, val){
      state.tableData = val;
    },
    set_usernameSave(state, val){
      state.username = val;
      localStorage.setItem('username', val)
    },
    set_passwordMD5Save(state, val){
      state.passwordMD5 = val;
      localStorage.setItem('passwordMD5', val)
    },
    set_chooseSavePassword(state, val){
      state.chooseSavePassword = val;
      localStorage.setItem('chooseSavePassword', val)
    }
  },
  actions: {},
  getters: {
    isLoggedIn(state){
      return !(state.token===''||state.token==='undefined');
    },
  },
});

export default store;